<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/11
 * Time: 16:45
 */
//开启http服务
//$http = new Swoole\Http\Server("0.0.0.0", 9501);
//$http->on('request', function (Swoole\Http\Request $request,Swoole\Http\Response  $response) {
//    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
//});
//$http->start();


//创建一个协程http请求
//go(function () {
//    $cli = new \Swoole\Coroutine\Http\Client('node3.web3api.com', 80);
//    $cli->set(['timeout'=>5]);//设置请求时间不能大于5秒
//    $cli->setHeaders(['Content-Type' => "application/json",]);//设置请求头
//    $cli->setDefer();//延迟收包
//    $data =[];//请求参数
//    $cli->post('/',json_encode($data));
//    $cli->recv();//接收包
//    $cli->body;//获取接收文本信息
//});


//开启WebSocket 服务
$server = new Swoole\WebSocket\Server("0.0.0.0", 9505);

$server->on('open', function (Swoole\WebSocket\Server $server, $request) {
    echo "server: handshake success with fd{$request->fd}\n";
});

$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $server->push($frame->fd, "this is server");
});

$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed\n";
});
$server->start();
